<?php


namespace app\services;


use app\models\Users;
use app\models\UsersProduct;

class UsersProductService extends Services {
    protected $limit;
    protected $pageSize;
    protected $page;
    protected $orderBy = 'order_time DESC,id ASC';
    protected $Where = [];
    protected $andWhere = [];

    /**
     * @return mixed
     */
    public function getLimit() {
        return $this->limit;
    }

    /**
     * @param mixed $limit
     */
    public function setLimit($limit) {
        $this->limit = $limit;
    }

    /**
     * @return int
     */
    public function getPageSize() {
        return $this->pageSize;
    }

    /**
     * @param int $pageSize
     */
    public function setPageSize($pageSize) {
        $this->pageSize = $pageSize;
    }

    /**
     * @return int
     */
    public function getPage() {
        return $this->page;
    }

    /**
     * @param int $page
     */
    public function setPage($page) {
        $this->page = $page;
    }

    /**
     * @return string
     */
    public function getOrderBy() {
        return $this->orderBy;
    }

    /**
     * @param string $orderBy
     */
    public function setOrderBy($orderBy) {
        $this->orderBy = $orderBy;
    }

    public function setWhere($fix = '<>', $key, $value) {
        $this->Where = [
            $fix, $key, $value,
        ];
    }

    public function setAndWhere($Element = []) {
        $this->andWhere = $Element;
    }

    public function getAndWhere() {
        return $this->andWhere;
    }

    public function getList() {
        return UsersProduct::find()
            ->where($this->Params)
            ->andWhere($this->andWhere)
            ->orderBy($this->orderBy)
            ->offset($this->page)
            ->limit($this->pageSize)
            ->all();
    }

    public function getCount() {
        return UsersProduct::find()
            ->where($this->Params)
            ->andWhere($this->andWhere)
            ->count();
    }

    public function getOne() {
        return UsersProduct::findOne($this->Params);
    }

    public function getAll() {
        return UsersProduct::find()
            ->where($this->Params)
            ->all();
    }

    public function addUsersProdcut() {
        $UsersProduct = new UsersProduct();
        $UsersProduct->user_id = $this->Params['user_id'];
        $UsersProduct->product_id = $this->Params['product_id'];
        $UsersProduct->ctime = date("Y-m-d H:i:s");
        $UsersProduct->utime = date("Y-m-d H:i:s");
        $UsersProduct->trad_price = isset($this->Params['trad_price']) ? $this->Params['trad_price'] : 0;
        $UsersProduct->adopted_price = isset($this->Params['adopted_price']) ? $this->Params['adopted_price'] : 0;
        $UsersProduct->trad_date = isset($this->Params['trad_date']) ? $this->Params['trad_date'] : date("Y-m-d H:i:s");
        $UsersProduct->type = isset($this->Params['type']) ? $this->Params['type'] : 1;
        return $UsersProduct->save();
    }

    //
    public function getTimeByProduct() {
        return UsersProduct::find()
            ->where($this->Params)
            ->orderBy('order_time DESC,id ASC')
            ->andWhere($this->andWhere)
            ->one();
    }
}